﻿using Microsoft.AspNet.Identity;
using SendGrid;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
//using System.Net;
using System.Net.Mail;
//using System.Web.Mail;

namespace ezCloud.Api.Services
{
    public class EmailService : IIdentityMessageService
    {
        public async Task SendAsync(IdentityMessage message)
        {
            await configSendGridasync(message);
        }

        // Use NuGet to install SendGrid (Basic C# client lib) 
        private async Task configSendGridasync(IdentityMessage message)
        {
            var myMessage = new SendGridMessage();

            //SendGrid myMessage = SendGrid.GetInstance();
            //SendGridMessage emailMessage = new SendGridMessage();

            myMessage.AddTo(message.Destination);
            myMessage.From = new System.Net.Mail.MailAddress("vuongtx1989@yahoo.com", "Trinh Vuong");
            myMessage.Subject = message.Subject;
            myMessage.Text = message.Body;
            myMessage.Html = message.Body;

            var credentials = new NetworkCredential(ConfigurationManager.AppSettings["mailAccount"],
                                                    ConfigurationManager.AppSettings["mailPassword"]);

            // Create a Web transport for sending email.
            var transportWeb = new Web(credentials);

            // Send the email.
            if (transportWeb != null)
            {
                await transportWeb.DeliverAsync(myMessage);
            }
            else
            {
                //Trace.TraceError("Failed to create Web transport.");
                await Task.FromResult(0);
            }
        }

        public async void SendGmailAsync(IdentityMessage message)
        {
            configGmailAsync(message);
        }

        private async void configGmailAsync(IdentityMessage message)
        {

            var fromAddress = new MailAddress(ConfigurationManager.AppSettings["mailAccount"], "ezCloud Hotel Team");
            var toAddress = new MailAddress(message.Destination, "");
            string fromPassword = ConfigurationManager.AppSettings["mailPassword"];
            string subject = message.Subject;
            string body = message.Body;
            
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var myMessage = new MailMessage(fromAddress, toAddress)
            {
                
                Subject = subject,
                Body = body, IsBodyHtml = true, 
                
                
            })
            {
                
                if (ConfigurationManager.AppSettings["RootUrl"].Contains("localhost"))
                {
                   // myMessage.Bcc.Add(new MailAddress("vuongtx1989@yahoo.com", "Vuong"));
                   // myMessage.Bcc.Add(new MailAddress("huydq5000@gmail.com", "Mr CTO"));
                } else
                {
                    myMessage.Bcc.Add(new MailAddress("dung.nguyen@ezcloud.vn", "ezCloud Founders"));
                    myMessage.Bcc.Add(new MailAddress("long.nguyen@ezcloud.vn", "ezCloud Developer"));
                   // myMessage.Bcc.Add(new MailAddress("partner@ezcloud.vn", "ezCloud Partner"));
                }
                    
                smtp.Send(myMessage);
            }
        }
    }
}